class TestObj:
    def __init__(self, name, age):
        self.name=name
        self.age=age

    def test(self):
        print("执行test方法")

    def play(self, adr):
        print("{}在{}玩游戏".format(self.name, adr))

def a():
    print("类外部方法")

if __name__ == '__main__':
    xiaoyu = TestObj("小宇", 20)
    xiaoyu.test()
    setattr(xiaoyu, "test", a)
    xiaoyu.test()
    # print(xiaoyu.name)
    # # delattr()删除指定的属性
    # delattr(xiaoyu, "name")
    # # setattr(xiaoyu, "name", "winnie")  # 修改属性的值
    # # setattr(xiaoyu, "name", "小红") ==
    # print(hasattr(xiaoyu, "name"))
    # print(xiaoyu.name)
    # print(xiaoyu.name)
    # getattr()能够获取对象中的属性
    # while True:
    #     value = input("请输入你想获取的属性:")
    #     print(getattr(xiaoyu, value))
    # getattr()能够获取对象中的方法
    # getattr(xiaoyu, "test")()
    # while True:
    #     func_value = input("请输入你想执行的方法:")
    #     # 判断对象是否有对应的属性及方法, 返回True或False
    #     if hasattr(xiaoyu, func_value):
    #         adr = input("请输入地点:")
    #         getattr(xiaoyu, func_value)(adr)
    #     else:
    #         print("您输入的方法不存在!")